home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 247_01 / bnio2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-17  |  2.1 KB  |  101 lines

  1. /*
  2.  *   MIRACL I/O routines 2. 
  3.  *   bnio2.c
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "miracl.h"
  8.  
  9. /* Access global variables */
  10.  
  11. extern small  base;     /* number base      */
  12. extern small  apbase;   /* apparent base    */
  13. extern int    depth;    /* error tracing .. */
  14. extern int    trace[];  /* .. mechanism     */
  15.  
  16. extern big w1;        /* workspace variables */
  17. extern big w2;
  18. extern big w3;
  19. extern big w4;
  20. extern big w5;
  21. extern big w6;
  22.  
  23. void cbase(x,oldbase)
  24. big x;
  25. small oldbase;
  26. {  /*  change radix of x from oldbase to base  */
  27.     int i,s;
  28.     bool done;
  29.     if (ERNUM || base==oldbase) return;
  30.     depth++;
  31.     trace[depth]=13;
  32.     if (TRACER) track();
  33.     s=exsign(x);
  34.     numer(x,w1);
  35.     insign(PLUS,w1);
  36.     done=FALSE;
  37.     forever
  38.     {
  39.         zero(w6);
  40.         convert((small)1,w3);
  41.         for (i=1;i<=w1[0];i++)  
  42.         {
  43.             premult(w3,w1[i],w2);
  44.             add(w6,w2,w6);
  45.             premult(w3,oldbase,w3);
  46.         }
  47.         if (ERNUM || done) break;
  48.         denom(x,w1);
  49.         copy(w6,w5);
  50.         done=TRUE;
  51.     }
  52.     round(w5,w6,x);
  53.     insign(s,x);
  54.     depth--;
  55. }
  56.  
  57. int cinnum(x,filep)
  58. flash x;
  59. FILE *filep;
  60. {  /*  input big number in base IOBASE  */
  61.     small newb,oldb,b;
  62.     int ipt;
  63.     if (ERNUM) return 0;
  64.     depth++;
  65.     trace[depth]=14;
  66.     if (TRACER) track();
  67.     newb=IOBASE;
  68.     oldb=apbase;
  69.     setbase(newb); /* temporarily change base ... */
  70.     b=base;
  71.     ipt=innum(x,filep); /* ... and get number  */
  72.     setbase(oldb);      /* restore original base */
  73.     cbase(x,b);
  74.     depth--;
  75.     return ipt;
  76. }   
  77.  
  78. int cotnum(x,filep)
  79. flash x;
  80. FILE *filep;
  81. {  /*  output a big number in base IOBASE  */
  82.     small newb,oldb,b;
  83.     int ipt;
  84.     if (ERNUM) return 0;
  85.     depth++;
  86.     trace[depth]=15;
  87.     if (TRACER) track();
  88.     newb=IOBASE;
  89.     oldb=apbase;
  90.     b=base;
  91.     setbase(newb);  /* temporarily change base ... */
  92.     cbase(x,b);
  93.     ipt=otnum(x,filep); /* ... and output number  */ 
  94.     b=base;
  95.     setbase(oldb);
  96.     cbase(x,b);     /* change back */
  97.     depth--;
  98.     return ipt;
  99. }
  100.  
  101.